import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const info = {
	id: "",
	nickname: "",
	head_portrait: "",

	role_id: "",
	address: "",

	real_name: "",
	real_card: "",

	phone: "",
	show_phone: "",

	silver: 0.00,
	// balance: 0.00,
	invitation_code: null
};
const disaster = {
	name: "",
	img: ""
};
const store = new Vuex.Store({
	state: {
		loadmoreColor: "#FFF",
		token: uni.getStorageSync('token') || "",
		info: uni.getStorageSync('info') || info,
		disaster: uni.getStorageSync('disaster') || disaster,
		url: uni.getStorageSync('url') || "",
		env: uni.getStorageSync('env') || "",
		tabbarIndex: uni.getStorageSync('tabbarIndex') || 0,
	},
	mutations: {
		changeTabbarIndex(state, payload) {
			state.tabbarIndex = payload;
			uni.setStorageSync('tabbarIndex', payload);
		},
		changeInfo(state, payload) {
			state.info = payload;
			uni.setStorageSync('info', payload);
		},
		changeDisaster(state, payload) {
			state.disaster = payload;
			uni.setStorageSync('disaster', payload);
		},
		changeToken(state, payload) {
			state.token = payload;
			let env = "h5";
			if (window.top.android) {
				env = "android";
				window.top.android.getData(payload);
			}
			if (window.top.webkit) {
				env = "ios";
				window.top.webkit.messageHandlers.getData.postMessage(payload)
			}
			state.env = env;
			uni.setStorageSync('env', env);
			uni.setStorageSync('token', payload);
		},
		changeUrl(state, payload) {
			state.url = payload;
			uni.setStorageSync('url', payload);
		},
		outLogin(state, payload) {
			state.token = null;
			state.info = info;
			state.disaster = disaster;
			uni.removeStorageSync("info");
			uni.removeStorageSync("token");
			uni.removeStorageSync("disaster");
		}
	}
})

export default store